﻿function copyImage(c){
var s = $('#textComment').val() + ' ' + c + '';
$('#textComment').val(s);
}
var curPageComment = 1;
function stripslashes (str) {
	  return (str + '').replace(/\\(.?)/g, function (s, n1) {
	    switch (n1) {
	    case '\\':
	      return '\\';
	    case '0':
	      return '\u0000';
	    case '':
	      return '';
	    default:
	      return n1;
	    }
	 });
}
function loadPageComment(page){
	var pid =  parseInt($('#product_id').val());
	var isLogin = $("#isLogin").val();
	$('#page_comment_current').val(page);
	if(typeof(pid) !== 'undefined' ){	
		var limit =$('#limit').val();
		$('#ListComment').append("<li style='text-align:center;padding-top:15px;' id='loadingComment'><img src='/images/loadingAnimation.gif' alt='' /></li>");
		$.ajax({
			type: "GET",
			dataType: 'json',
			url: "/mg/comment/getcomment",
			// you can use an object here
			data: { pid: pid, page: page, limit: limit },
			success: function(rep) {
				var html = '';
				var comments = rep.comments;
				if(rep.total > 0){
					$.each(comments,function(){
					
						html += '<li>' +
								'	<span class="avarta" id="image2_' + this.id +'" > </span>  ' +
								' <div class="Comment"> ' +
								'<h3 id="user2_' + this.id + '"> </h3>' +
								'<p>' + stripslashes(this.content) + '</p>' +
								'<p class="Date">' + this.time + '</p>' +
								'</div>' +
								'<div class="clear"></div>' +
								'<div class="cmt-arrow"></div>' +
								'</li>';
						e =  'image2_'  + this.id ;
						f = 'ZMEA_' + this.username + '?l=2&width=49&height=49';
						zwg.addWgItem(new wgItem(e, f));
						e =  'user2_'  + this.id ;
						f = 'ZMED_' + this.username + '?l=2';
						zwg.addWgItem(new wgItem(e, f));
						
					});
					if(html != ''){
						//$('#pagination').html(rep.pagination);
						$('#ListComment').append(html);
						curPageComment += 1;
						$('#loadingComment').remove();
						var totalPage = parseInt($('#total_page').val());
						if(curPageComment >= totalPage){
							$('.cmt-more').remove();
						}
						
					}
					var opt = {
							handle: '#etoggle',
							dir: imgUrl + '/mg/images/emotions/',
							label_on: 'On Emotions',
							label_off: 'Off Emotions',
							style: 'background: #eee',
							css: 'class2'
						}
					$('.emotion').emotions2(opt);
					zwg.fillWg();
				}
			}
		});
		
	}
}

function login(link){ //file extension.js, function $(".Login")
	
	//var link = $(this).attr('href');
	//	$('#dialog').attr("class","");
		$('#dialog').attr("style","");
		// alert("s");
		var d = $('#dialog').html('<iframe id="ifrm"  scrolling="no" frameborder="0" ALLOWTRANSPARENCY="true" bgcolor="#eeeeee" style="width:100%; height:100%; border:none; overflow:hidden;"></iframe>');
			d.dialog({						
			autoResize: true,
			modal: true	,
			height: 287,
			width: 330,	
			open: function (event,ui) {$(this).css("width","100%")}			
		});
		$("#dialog>#ifrm").attr("src", link);
		
		$("#dialog").prepend('<a href="#" class="BtClose" title="Đóng">Đóng</a>');
		
		$(".BtClose").live('click',function(){
			d.dialog( "close" );
		});	
		
		return false;
	

}

function popupNeedLogin(link){
//		var d = $('#dialog').html('<iframe id="ifrm"  scrolling="no" frameborder="0" ALLOWTRANSPARENCY="true" bgcolor="#eeeeee" style="width:100%; height:100%; border:none; overflow:hidden;"></iframe>');
//		$('#dialog').dialog({						
//			autoResize: true,
//			modal: true	,
//			height: 600,
//			width: 545,	
//			open: function (event,ui) {$(this).css("width","100%")}			
//		});
//		$("#dialog>#ifrm").attr("src", link);
//		
//		$("#dialog").prepend('<a href="#" class="BtClose" title="Đóng">Đóng</a>');
//		
//		$(".BtClose").live('click',function(){
//			d.dialog( "close" );
//			return false;
//		});	
		mg.login.showPopup();
		return false;
}
function closeDialog(){
	var d = $('#dialog');
	d.dialog( "close" );
	checklogin();
}
$(document).ready(function(){
	if ( controller == "detail" && action != "cat") {
		loadAvatarComment();
	}
	var clickDisabled = false;
	var pid = $('#product_id').val();
	var isRating = 0;
	var isLogin = $("#isLogin").val();

	jQuery('#frmCommentSubmit').click(function(){ 
		var urlPost = "/mg/comment/addcomment";
		jQuery('#comment_error').hide();		
		if (clickDisabled){
			jQuery('#comment_error').attr('class','warning');
			jQuery('#comment_error').html('Bạn vừa bình luận, vui lòng chờ trong giây lát.');
			jQuery('#comment_error').show();			
			return false;
		}
		var username = "";
		if(isLogin == 0){
			username = $('#username').val();
			if(username == 'undefined' || jQuery.trim(username) == ""){
				jQuery('#comment_error').attr('class','warning');
				jQuery('#comment_error').html('Bạn phải đăng nhập để sử dụng chức năng bình luận!');
				jQuery('#comment_error').show();
				return false;				
			}
		}
		
		var txtaComment = jQuery.trim($('#textComment').val());
		if(txtaComment == ""){
			jQuery('#comment_error').attr('class','warning');
			jQuery('#comment_error').html('Vui lòng nhập nội dung bình luận!');
			jQuery('#comment_error').show();
			return false;
		}else if(txtaComment.length < 10){
			jQuery('#comment_error').attr('class','warning');
			jQuery('#comment_error').html('Nội dung phải có nhiều hơn 10 kí tự.');
			jQuery('#comment_error').show();
			return false;
		}
		var pid = $('#product_id').val();
		var rate = isRating;
		var keyValidate = $('#keyValidate').val();
		var timeWait = parseInt($('#flood').val());
		//do action comment here
		$.ajax({  
			type: "POST",
			dataType: 'json', 
			url: urlPost, 
			// you can use an object here
			data: { txtaComment: txtaComment , pid : pid, rating : rate, uname : username ,key: keyValidate},
			success: function(res) {  		
				if(res.status == 1){			
					jQuery('#comment_error').attr('class','success');
					$('#textComment').val('');
					clickDisabled = true;
					isRating = 0;
					setTimeout(function(){clickDisabled = false;jQuery('#comment_error').hide();},timeWait );						
				}else{
					jQuery('#comment_error').attr('class','warning');
				}
				if(res.msg === "login"){
					//var link =  $('#page_login').val();
					//login(link);
					popupNeedLogin();
					//jQuery('#comment_error').html("Bạn phải đăng nhập để sử dụng chức năng bình luận!");
					//jQuery('#comment_error').show();
				}
				else{
				jQuery('#comment_error').html(res.msg);
				jQuery('#comment_error').show();
				}
			}
		});		

		return false;
	});
	$('.cmt-more').click(function(e){
		var totalPage = parseInt($('#total_page').val());
		if(curPageComment < totalPage){
			loadPageComment(curPageComment + 1);
		}
		return false;
	});
})

